

use "${newdata}car_owner_event", clear
compress

* === Fixing problem 1: some cars have freg_DMY AFTER event_DMY ==================

* Saving the event date for these observations
gen aux = event_DMY if freg_DMY > event_DMY
* But only those where event type is "new registration"
replace aux = . if event_type != 1
* Generating a variable that saves the event date for all obs with unique carids
bys carid: egen aux2 = mean(aux)
* Replacing the first registration variable with this observation
replace freg_DMY = aux2 if aux2 != .
/* Now, freg_DMY - event_DMY = 0 for all obs where event_type == 1, as it should */
drop aux aux2

* ==============================================================================
* Adding YEAR as a variable
sort carid event_DMY event_end_DMY event_type
* We dont need to keep events ending before 2005 anymore
drop if event_end_DMY < mdy(1,1,2005)

gen double eventid = _n /* Note: "double" is necessary here */
* Number of years per event
gen years_temp = year(event_end_DMY) - year(event_DMY) + 1
* Expanding each event by number of years
expand years_temp
drop years_temp
* Generating variable for year
bysort eventid: gen year = year(event_DMY[1]) + _n - 1
* Number of "active" days the first year of an event
bysort eventid (year): ///
	gen days = min(event_end_DMY, mdy(12,31,year)) - event_DMY + 1 if _n == 1
* Number of "active" days the last year of an event
bysort eventid (year): ///
	replace days = event_end_DMY - mdy(1,1,year) if _n == _N & missing(days)
* Number of active days in the middle (number of days of the year)
replace days = mdy(12,31,year) - mdy(1,1,year) + 1 if missing(days)
* We dont need years prior to 2005
drop if year < 2005	

order year, b(carid)
drop event_days_use days_use_cum event_nr_bw event_days days_cum days_use_prior
compress

* Days the car has been in use each year*event =================================
gen int daysused = days
replace daysused = 0 if event_type == 3
* Extrapolating kmperday for the missing part of each year
bys carid year: egen aux = mean(kmperday) /* this var is also used 6 lines from now */
replace kmperday = aux if kmperday == .
*kms driven each year*event
gen vkt = daysused * kmperday
replace vkt = 0 if daysused == 0
* Indicator for whether vkt should be missing after collapse
bys carid year: gen vkt_missing = (aux == .)
drop aux

* === Indicators for types of car decisions ====================================

* Indicator for "new purchase" the first year a car has event type == 1
bys carid (year event_DMY event_end_DMY event_type): ///
	gen new = (event_type == 1 & _n == 1)

* Indicator for "used import" the first year a car has event type == 2	
bys carid (year event_DMY event_end_DMY event_type): ///
	gen imp = (event_type == 2 & _n == 1)

* Indicator for "export" the last year a car has event type == 6	
bys carid (year event_DMY event_end_DMY event_type): ///
	gen exp = (event_type_end == 6 & _n == _N)

* Indicator for "scrapped" the last year a car has event type == 7	
bys carid (year event_DMY event_end_DMY event_type): ///
	gen scrapped = (event_type_end == 7 & _n == _N)

* Mark the car as "sold used" if the owner changes to the next observation
bys carid (year event_DMY event_end_DMY event_type): ///
	gen sold = (ownerid != ownerid[_n+1] & carid == carid[_n+1])
	
* Mark the car as "purchased used" if the owner changes from the previous observation	
bys carid (year event_DMY event_end_DMY event_type): ///
	gen usedpch = (ownerid != ownerid[_n-1] & carid == carid[_n-1])

	
* === Collapsing events together ===============================================		

// Note - in "collapse", stata treats missing values as zeroes
compress

collapse  (last) freg_DMY fuel sp_skilt rutine /// This command takes a VERY long time
				 kj_art kjt_grup typekode stjaalet utfoert bruktimp ///
				 freg_date ownertype ownerid ///
		   (sum) daysused days vkt new imp exp scrapped sold usedpch vkt_missing ///
		  (mean) kmperday ///
	, by(carid year)
compress

drop if days == 0
drop if daysused == 0
* ==============================================================================

/* vkt is set to zero when it should be missing due to the 
nature of the collape command. fix this */
replace vkt = . if vkt_missing > 0
replace vkt = . if kmperday > 0 & daysused > 0 & vkt == 0 
* Cars cannot be "purchased new"/"imported" and "purchased used" by the same ind in the same year
replace usedpch = 0 if new == 1 
replace usedpch = 1 if usedpch > 1
replace usedpch = 0 if imp == 1
* If cars are both sold and scrapped/exported, keep scrapped/exported
replace sold = 0 if scrapped == 1 | exp == 1

* Collecting all car acqusition choices in one variable ========================
gen car_acq = 0
replace car_acq = 1 if new > 0
replace car_acq = 2 if imp > 0
replace car_acq = 3 if usedpch > 0

* Fixing error in new purchases for first year
replace car_acq = 0 if car_acq == 1 & year - year(freg_DMY) != 0

label var car_acq "Car acquisition"
label define aquisition ///
1 "New purchase" ///
2 "Import" ///
3 "Used purchase", replace
label values car_acq aquisition

* Collecting all car disposal choices in one variable ==========================
gen car_dis = 0
replace car_dis = 1 if scrapped > 0
replace car_dis = 2 if exp > 0
replace car_dis = 3 if sold > 0
label var car_dis "Car disposal"
label define disposal ///
	1 "Scrapped" ///
	2 "Exported" ///
	3 "Used sale", replace
label values car_dis disposal

drop new imp usedpch scrapped exp sold

label var fuel "Fuel type"
label define fuel ///
	0 "Unknown" ///
	1 "Gasoline" ///
	2 "Diesel" ///
	3 "Kerosene" ///
	4 "Gas" ///
	5 "BEV" ///
	6 "Hydrogen" ///
	7 "Gasoline hybrid" ///
	8 "Diesel Hybrid" ///
	9 "Other", replace
label values fuel fuel

label var sp_skilt "License plate type"
label define licenseplate ///
	0 "Standard" ///
	1 "Closed area" ///
	2 "Temporary" ///
	3 "Embassy" ///
	4 "Vans" ///
	5 "Rally", replace
label values sp_skilt licenseplate

label var freg_DMY "First registration date"
label var kjt_grup "Vehicle group"
replace kjt_grup = 106 if kjt_grup == 107 /* Both ambulances */
replace kjt_grup = 310 if kjt_grup == 314 /* Both vans category 1 */
replace kjt_grup = 311 if kjt_grup == 315 /* Both vans category 2 */
replace kjt_grup = 620 if kjt_grup == 621 /* Not separating betw heavy motorcycle
											 and chopper */

label define vhclcat ///
	101 "Passenger car" ///
	106 "Passenger car (ambulance)" ///
    310 "Van, cat. 1" ///
    311 "Van, cat. 2" ///
    312 "Hearse" ///
    601 "Moped" ///
    610 "Light motorcycle" ///
    620 "Heavy motorcycle" ///
    630 "Belt motorcycle", replace
label values kjt_grup vhclcat 

label var kj_art "Driving category"
replace kj_art = 10 if kj_art == 19 /*own transport*/
/* Scheduled service vehicles grouped together below */
replace kj_art = 20 if kj_art == 21 | kj_art == 22 | kj_art == 23 | kj_art == 24 ///
	| kj_art == 25 | kj_art == 26
/* All taxies grouped together below, as well as 34: Selskapsvogn 
	and 80-82: Turvogn */
replace kj_art = 30 if kj_art == 31 | kj_art == 32 | kj_art == 33 | kj_art == 34 ///
	| kj_art == 80 | kj_art == 81 | kj_art == 82
replace kj_art = 90 if kj_art == 99 /* Other licensable transport */
	
label define drivingcat ///
	10 "Own transport" ///
	11 "Own transport, diplomat" ///
	12 "Own transport, military" ///
	13 "Own transport, police" ///
	14 "Own transport, Nato" ///
	15 "Own transport, royal" ///
	16 "Own transport, handicapped" ///
	17 "Only allowed on Svalbard" ///
	18 "Nordic Investment Bank" ///
	20 "Scheduled service" ///
	30 "Taxi" ///
	40 "Used in licensable freight transport" ///
	50 "Rental/leasing" ///
	60 "Approved for driving schools" ///
	70 "For hotel guests" ///
	90 "Other licensable transport" ///
	91 "Other licensable transport, handicapped" ///
	92 "Other licensable transport, funeral", replace
label values kj_art drivingcat

label define ownertype 0 "Firm" 1 "Individual", replace
label values ownertype ownertype 

label define fregdate 0 "Y" 1 "DMY", replace
label values freg_date fregdate

drop bruktimp utfoert vkt_missing rutine
label var typekode "Type"
label var year "year"
label var carid "Car ID"
label var ownerid "Owner ID (P = ind, O = firm)"
label var stjaalet "Stolen (variable not used)"
label var freg_date "First registration DMY or Y"
label var ownertype "Owner type"
label var daysused "Operational days (car*owner*year)"
label var days "Days owned (car*owner*year)"
label var vkt "Vehicle kilometers travelled (car*owner*year)"
label var kmperday "Mean km per operational day"

order ownerid, a(carid)
order ownertype car_acq car_dis vkt kmperday daysused days, a(ownerid)
order kj_art kjt_grup, a(fuel)

/* marking passenger vehicles used for own transport */
gen car_valid = ((kj_art == 10 | kj_art == 16) & kjt_grup < 600)
compress

save "${newdata}car_stock_end_of_year", replace

* ==========================================================

/* Note - in order to ensure the stock is actual at the end of the year, 
   cars marked with car_dis == 1 (scrapped) or car_dis == 2 (exported) 
   must also be dropped. This is done on the next do-file before merging */
